package com.ruoyi.project.module.banner.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.constant.BusinessType;
import com.ruoyi.project.module.banner.domain.Banner;
import com.ruoyi.project.module.banner.service.IBannerService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import org.springframework.web.multipart.MultipartFile;

/**
 * 电影广告图 信息操作处理
 * 
 * @author frey
 * @date 2018-09-28
 */
@Controller
@RequestMapping("/module/banner")
public class BannerController extends BaseController
{
	private static final Logger log = LoggerFactory.getLogger(BannerController.class);
    private String prefix = "module/banner";
	
	@Autowired
	private IBannerService bannerService;

	@Value("${ruoyi.profile}")
	private String uploadUrl;


	@Value("${ruoyi.urlAddress}")
	private String urlAddress;



	@RequiresPermissions("module:banner:view")
	@GetMapping()
	public String banner()
	{
	    return prefix + "/banner";
	}
	
	/**
	 * 查询电影广告图列表
	 */
	@RequiresPermissions("module:banner:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Banner banner)
	{
		startPage();
        List<Banner> list = bannerService.selectBannerList(banner);
		return getDataTable(list);
	}
	
	/**
	 * 新增电影广告图
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存电影广告图
	 */
	@RequiresPermissions("module:banner:add")
	@Log(title = "电影广告图", action = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Banner banner,@RequestParam(value = "bannerfile",required = false) MultipartFile file)
	{
		try {
			if (!file.isEmpty())
			{
				String carouselUrl = FileUploadUtils.upload(uploadUrl,file);
				banner.setImage(urlAddress+carouselUrl);
				return toAjax(bannerService.insertBanner(banner));
			}
			return error();
		} catch (IOException e) {
			log.error("上传一键记账轮播图失败！", e);
			return error();
		}

	}

	/**
	 * 修改电影广告图
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Banner banner = bannerService.selectBannerById(id);
		mmap.put("banner", banner);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存电影广告图
	 */
	@RequiresPermissions("module:banner:edit")
	@Log(title = "电影广告图", action = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Banner banner,@RequestParam(value="bannerfile",required = false) MultipartFile file)
	{
		try {
			if (file==null)
			{
				return toAjax(bannerService.updateBanner(banner));

			}else{
				String carouselUrl = FileUploadUtils.upload(uploadUrl,file);
				banner.setImage(urlAddress+carouselUrl);
				return toAjax(bannerService.updateBanner(banner));
			}

		} catch (IOException e) {
			log.error("上传一键记账轮播图失败！", e);
			return error();
		}


	}
	
	/**
	 * 删除电影广告图
	 */
	@RequiresPermissions("module:banner:remove")
	@Log(title = "电影广告图", action = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(bannerService.deleteBannerByIds(ids));
	}
	
}
